#include "DebugStatusLabel.h"

#include <QMetaEnum>
#include <QStyle>
#include <QTextDocument>

DebugStatusLabel::DebugStatusLabel(QStatusBar* parent) : QLabel(parent) {
#if VMP_STUDIO
  mStatusTexts[0] = tr("Analyzing");
  mStatusTexts[1] = tr("Ready");
  mStatusTexts[2] = tr("Initializing");
  mStatusTexts[3] = tr("Closed");
#else
  mStatusTexts[0] = tr("Initialized");
  mStatusTexts[1] = tr("Paused");
  mStatusTexts[2] = tr("Running");
  mStatusTexts[3] = tr("Terminated");
#endif
  QFontMetrics fm(this->font());
  int maxWidth = 0;
  for (size_t i = 0; i < _countof(mStatusTexts); i++) {
    int width = fm.width(mStatusTexts[i]);
    if (width > maxWidth) maxWidth = width;
  }
  this->setTextFormat(Qt::RichText);  // rich text
  this->setFixedHeight(fm.height() + 5);
  this->setAlignment(Qt::AlignCenter);
  this->setFixedWidth(maxWidth + 10);
  connect(Bridge::getBridge(), SIGNAL(dbgStateChanged(DBGSTATE)), this,
          SLOT(debugStateChangedSlot(DBGSTATE)));
}

QString DebugStatusLabel::state() const { return this->mState; }

void DebugStatusLabel::debugStateChangedSlot(DBGSTATE state) {
  const char* states[4] = {"initializing", "paused", "running", "stopped"};

  this->setText(mStatusTexts[state]);
  this->mState = states[state];

  if (state == stopped) {
    GuiUpdateWindowTitle("");
  }

#if !VMP_STUDIO
  this->style()->unpolish(this);
  this->style()->polish(this);
#endif
  this->update();
}
